const express = require('express')
const app = express()

const bodyParser = require('body-parser')
    //导入解析token中间件
const expressJwt = require('express-jwt')
    //cors 解决跨域问题
const cors = require('cors')
app.use(cors())
    // 静态资源托管
app.use('/tem_uploads', express.static('tem_uploads'))

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))


// app.use(express.urlencoded({ extended: false }))

//优化res.send()
//相应数据的中间件
// 响应数据的中间件
app.use((req, res, next) => {
        res.ResData = function(err, status = 1, data = {}) {
            res.send({
                data,
                meta: {
                    status,
                    msg: err instanceof Error ? err.message : err
                }
            })
        }
        next()
    })
    //     //身份认证，以/api开始请求的接口不需要token认证
    // app.use(expressJwt({ secret: 'secret' }).unless({ path: [/^\/api\//] }))
    //     //定义错误中间件
    // app.use((err, req, res, next) => {
    //         //获取捕获信息
    //         if (err.name === 'UnauthorizedError')
    //             return res.ResData("身份认证失败")
    //     })

//导入并注册用户路由模块
const userRouter = require('./router/user')
app.use('/api/private/v1', userRouter)
    //导入并注册
const menuRouter = require('./router/menu')
app.use('/api/private/v1', menuRouter)
    //导入并注册
const usersRouter = require('./router/Users')
app.use('/api/private/v1', usersRouter)

const authorization = require('./router/authorization')
app.use('/api/private/v1', authorization)

const catagories = require('./router/categories')
app.use('/api/private/v1', catagories)
const uploadRouter = require('./router/upload')
app.use('/api/private/v1', uploadRouter)

const reports = require('./router/reports')
app.use('/api/private/v1', reports)
const goods = require('./router/goods')
app.use('/api/private/v1', goods)
const order = require('./router/order')
app.use('/api/private/v1', order)
    // const rolesRouter = require('./router/roles')
    // app.use('/api/private/v1', rolesRouter)

// const usersStateRouter = require('./router/usersState')
// app.use('/api/private/v1', usersStateRouter)

// const usersSearchRouter = require('./router/usersSearch')
// app.use('/api/private/v1', usersSearchRouter)

// const usersEditRouter = require('./router/Useredit.js')
// app.use('/api/private/v1', usersEditRouter)

// const usersDeleteRouter = require('./router/deleteUser')
// app.use('/api/private/v1', usersDeleteRouter)

//调用listen方法，开启指定端口的一个web服务器
app.listen(8888, () => {
    console.log("服务器已开启");
})